package com.xiaoshou.action;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;

import com.xiaoshou.util.MyUUID;

public class A_MyTest {
	public final static String indexDirForQuestion = "F:\\工作相关索引\\销售索引";
	public static void main(String[] args) throws Exception, IOException {
		/*File file = new File("C:\\Users\\Administrator\\Desktop\\打包文件\\打包文件\\F盘\\123.png");
		System.out.println(file.getPath());
		System.out.println(file.getPath().substring(file.getPath().lastIndexOf("."), file.getPath().length()));
		System.out.println(file.getPath().substring(0, file.getPath().lastIndexOf("F盘\\")+3));*/
		//file.renameTo(new File("C:\\Users\\Administrator\\Desktop\\打包文件\\打包文件\\F盘\\123.png"));
		
		
		//del();
		//addIndexer();
		
		/*IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(indexDirForQuestion)));// 打开索引
		IndexSearcher searcher = new IndexSearcher(reader);
		searchaaa(searcher,"人民");*/
		for (int i = 0; i < 10; i++) {
			System.out.println(MyUUID.createUUID().toString());
		}
		
	}
	public static void del(){
		
		
		IndexWriter writer = null;
		Analyzer sa = new SmartChineseAnalyzer(Version.LUCENE_40);
		IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_40, sa);
		Directory dir1 = null;
		try {
			dir1 = FSDirectory.open(new File(indexDirForQuestion));
			writer = new IndexWriter(dir1, iwc);
			Term term = new Term("id", "a1470d4d-d844-4e1c-a030-283ad1e44841");
			
			//Term term2 = new Term("questionname",);
			writer.deleteDocuments(term);
			writer.commit();
			writer.close();
		} catch (CorruptIndexException e) {
			e.printStackTrace();
			System.out.println("删除索引失败1");
		} catch (LockObtainFailedException e) {
			System.out.println("删除索引失败2");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("删除索引失败3");
			e.printStackTrace();
		} finally{
			try {
				if(writer!=null){
					writer.close();
					sa.close();
				}
			} catch (CorruptIndexException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	
	public static List<Document> searchaaa(IndexSearcher searcher, String q) throws ParseException, IOException{
		Analyzer analyzer = new SmartChineseAnalyzer(Version.LUCENE_40);
		String[] fields = { "id","questionname" };
		Occur[] occurs = new Occur[] { Occur.SHOULD,Occur.SHOULD };
		Query query = MultiFieldQueryParser.parse(Version.LUCENE_40, q, fields,
				occurs, analyzer);
		
		TopDocs result = searcher.search(query, searcher.getIndexReader().maxDoc());
		ScoreDoc[] hits = result.scoreDocs;
		List<Document> list = new ArrayList<Document>();
		for (int i = 0; i < hits.length; i++) {
			Document doc = searcher.doc(hits[i].doc);
			list.add(doc);
		}
		System.out.println("搜索list的长度\t→→→→\t"+list.size());
		System.out.println("+++++++++++++++++++++++++++++++++");
		for (Document document : list) {
			System.out.println(document);
		}
		
		analyzer.close();
		return null;
	}
	
	public static void addIndexer(){
		Analyzer sa = new SmartChineseAnalyzer(Version.LUCENE_40);
		IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_40, sa);
		iwc.setOpenMode(OpenMode.APPEND);
		IndexWriter writer = null;
		try {
			Directory dir1 = FSDirectory.open(new File(indexDirForQuestion));
			writer = new IndexWriter(dir1, iwc);

			FieldType ft = new FieldType();
			ft.setIndexed(true);
			ft.setStored(true);
			ft.setTokenized(true);
			
			FieldType ft2 = new FieldType();
			
			//ft2.setNumericType(NumericType.INT);
			ft2.setIndexed(true);
			ft2.setStored(true);
			ft2.setTokenized(false);

			Document doc = new Document();
			doc.add(new Field("id", "444444444444444444", ft2));
			doc.add(new Field("questionname", "人民医院的位置", ft));
			writer.addDocument(doc);
			writer.close();
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (LockObtainFailedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			try {
				if(writer!=null){
					writer.close();
				}
				if(sa!=null){
					sa.close();
				}
			} catch (CorruptIndexException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}
}
